package com.fernfx.xingtan.common.base;

import android.app.Activity;
import android.text.TextUtils;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.AppManager;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import static com.fernfx.xingtan.common.AppManager.getAppManager;

/**
 * Created by admin on 2018/8/15.
 * 处理提示性回调函数
 */
public abstract class BaseIRequestCallback implements IRequestCallback{
    @Override
    public abstract void onSuccessful(NetworkOKResult result);

    @Override
    public void onFailure(String errorMessage, int errorCode) {
        hideLoading();
        if (TextUtils.isEmpty(errorMessage)) {
            onNetworkError(errorMessage, errorCode);
            return;
        }

        final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
        if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())) {
            ToastUtil.showCentertoast(baseEntity.getMsg());
        }
    }

    @Override
    public void onNetworkError(String errorMessage, int errorCode) {
        hideLoading();
        ToastUtil.showCentertoast(R.string.network_error);
    }

    /*
     *关闭加载框
     */
    protected void hideLoading(){
        Activity activity = AppManager.getAppManager().currentActivity();
        if (activity instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity)activity;
            baseActivity.hideLoading();
        }

    }
}
